﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FWorkOfArt : FormBase
    {
        private EDM_AuctionContainer context;
        WorkOfArt workOfArt = null;
        Validations valid = new Validations();
        public enum ACTION { VIEW, SEARCH, MODIFY, CREATE, ADDTOPACKAGE, DELETE };
        ACTION actionActual = ACTION.VIEW;
        
         public FWorkOfArt(ACTION anAction)
        {
             InitializeComponent();
            actionActual = anAction;
        }

          public FWorkOfArt(ACTION anAction, EDM_AuctionContainer context)
        {
            InitializeComponent();
            actionActual = anAction;
            this.context = context;
        }
        private void FWorkOfArt_Load1(object sender, EventArgs e)
        {
            preparePanel();
        }
        private void preparePanel()
        {
            if (actionActual == ACTION.VIEW)
            {
                b_save.Visible = false;
                pnlSearchWorkOfArt.Visible = false;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }
            if (actionActual == ACTION.SEARCH)
            {
                b_save.Visible = false;
                pnlSearchWorkOfArt.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }

            if (actionActual == ACTION.DELETE)
            {
                b_save.Visible = false;
                pnlSearchWorkOfArt.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }
            else if (actionActual == ACTION.MODIFY)
            {
                b_save.Visible = true;
                this.ControlesHabilitados(false);
            }

            else if (actionActual == ACTION.CREATE)
            {
                b_save.Visible = true;
                this.ControlesHabilitados(true);
            }

            else if (actionActual == ACTION.ADDTOPACKAGE)
            {
                b_save.Visible = true;
                this.ControlesHabilitados(true);
            }
            if (actionActual == ACTION.SEARCH || actionActual == ACTION.MODIFY || actionActual == ACTION.ADDTOPACKAGE || actionActual == ACTION.DELETE)
            {
                FSearchWorkOfArt f_SearchWorkOfArt = new FSearchWorkOfArt(this, context);
                f_SearchWorkOfArt.TopLevel = false;
                this.pnlSearchWorkOfArt.Controls.Add(f_SearchWorkOfArt);
                f_SearchWorkOfArt.Show();
            }
        }

        private void LoadDataOfWorkOfArt(WorkOfArt entityWorkOfArt)
        {
            if (entityWorkOfArt != null)
            {
                t_nameAuthor.Text =entityWorkOfArt.AuthorName;
                t_nameWork.Text = entityWorkOfArt.nameOfWork;
                t_surname.Text = entityWorkOfArt.AuthorSurname;
                cb_Type.Text =entityWorkOfArt.typeOfWorkOfArt;
                t_year.Text = entityWorkOfArt.year.ToString();
            }
        }

        public override void TransferWorkFound(WorkOfArt aWork)
        {
                this.workOfArt = aWork;
                b_save.Visible = true;
                this.ControlesHabilitados(true);
                pnlSearchWorkOfArt.Visible = false;
                LoadDataOfWorkOfArt(workOfArt);

                if (actionActual == ACTION.DELETE || actionActual == ACTION.SEARCH || actionActual == ACTION.MODIFY)
                {
                    b_delete.Visible = true;
                }
                if (actionActual == ACTION.DELETE)
                {
                    b_save.Visible = false;
                }
        }

        private void b_exit_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Cancel);
            Close();
        }

        private void b_save_Click(object sender, EventArgs e)
        {
            Output(DialogResult.OK);
            Close();
        }

        private void Output(DialogResult Salida)
        {
            switch (Salida)
            {
                case DialogResult.OK:
                    if (actionActual == ACTION.CREATE)
                    {
                        workOfArt = new WorkOfArt();
                    }
                    workOfArt.AuthorName = t_nameAuthor.Text.ToString();
                    workOfArt.AuthorSurname = t_surname.Text.ToString();
                    workOfArt.typeOfWorkOfArt = cb_Type.Text.ToString();
                    workOfArt.nameOfWork = t_nameWork.Text.ToString();
                    workOfArt.year = valid.GetByte(t_year);
                    workOfArt.idWorkOfArt = 1;

                    if (actionActual == ACTION.CREATE)
                    {
                        context.WorkOfArtSet.AddObject(workOfArt);
                        context.SaveChanges();
                    }
                    else if (actionActual == ACTION.MODIFY)
                    {
                        context.WorkOfArtSet.ApplyCurrentValues(workOfArt);
                        context.SaveChanges();
                    }
                    MessageBox.Show("Work of art inserted", "New Work", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ((FormMainMenu)(this.ParentForm)).CloseAll();
                    break;

                case DialogResult.Cancel: MessageBox.Show("The insertion of the bidder has been cancelled", "Advise", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Abort:
                    context.WorkOfArtSet.DeleteObject(workOfArt);
                    context.SaveChanges();
                    MessageBox.Show("The auction has been deleted", "Client Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
            }
        }

        private void b_AddToPackage_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Retry);
            Close();
        }

        private void b_delete_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Abort);
            Close();
        }
    }
}
